﻿using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using System.Text;
using System.Threading.Tasks;

namespace HPT.CheckTicket.Sample.Controllers
{
    [ApiController]
    [Route("")]
    public class WeatherForecastController : ControllerBase
    {
        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        ///  <summary>
        /// 验票
        ///  </summary>
        ///  <param name="a"></param>
        ///  <param name="request"></param>
        ///  <param name="m"></param>
        ///  <returns></returns>
        [HttpPost]
        public async Task<IActionResult> checkticket([FromQuery] string m, [FromQuery] string a, [FromBody] object request)
        {
            if (string.IsNullOrWhiteSpace(m) || !"gateapi".Equals(m))
            {
                return Content(JsonConvert.SerializeObject(new GatealivecsResponse()
                {
                    status = "201",
                    errorMessage = "接口名无效！",
                }, Formatting.None), "application/json;charset=GBK",
                    Encoding.GetEncoding("GBK"));
            }

            if (string.IsNullOrWhiteSpace(a))
            {
                return Content(JsonConvert.SerializeObject(new GatealivecsResponse()
                {
                    status = "201",
                    errorMessage = "接口名无效！",
                }, Formatting.None), "application/json;charset=GBK",
                    Encoding.GetEncoding("GBK"));
            }

            var api = a.Trim().ToLower();
            switch (api)
            {
                case "gatealive":
                    //返回json数
                    return Content(JsonConvert.SerializeObject(new GatealivecsResponse
                        {
                        status = "200",
                        errorMessage = "成功！",
                    }, Formatting.None), "application/json;charset=GBK",
                        Encoding.GetEncoding("GBK"));
                case "checkticket":
                    //返回json数
                    return Content(JsonConvert.SerializeObject(new CheckticketResponse
                    {
                        checkResult = 1,
                        checkType = 0,
                        VoiceNum = 1,
                        checkMsg1 = "票类：成人票",
                        checkMsg2 = "票号：000000000001",
                        checkMsg3 = "到期：2017-12-01",
                        checkMsg4 = "验票成功，请过闸"
                    }, Formatting.None), "application/json;charset=GBK",
                        Encoding.GetEncoding("GBK"));
                case "cancellation":
                    //返回json数
                    return Content(JsonConvert.SerializeObject(new CancellationReponse
                    {
                        checkResult = "OK"
                    }, Formatting.None), "application/json;charset=GBK",
                        Encoding.GetEncoding("GBK"));

                default:
                    return Content(JsonConvert.SerializeObject(new GatealivecsResponse
                    {
                        status = "201",
                        errorMessage = "方法名无效！",
                    }, Formatting.None), "application/json;charset=GBK",
                        Encoding.GetEncoding("GBK"));
            }

            //if (a.ToLower().Equals("gatealive"))
            //{
            //    //   DataService.Instance.AddRecord($"\r\n");
            //    //var gatealive = JsonConvert.DeserializeObject<GatealivecsResquest>(request.ToString());
            //    //DataService.Instance.AddRecord($"收到心跳数据:{JsonConvert.SerializeObject(gatealive)}");

            //    var response = new GatealivecsResponse()
            //    {
            //        status = "200",
            //        errorMessage = "成功！",
            //    };

            //    //OBJ转化成JSON
            //    //string json = JsonConvert.SerializeObject(response);

            //    //  DataService.Instance.AddRecord($"\r\n");
            //    //DataService.Instance.AddRecord($"回复心跳数据:{json}");

            //    //返回json数
            //    return Content(JsonConvert.SerializeObject(response, Formatting.None), "application/json;charset=GBK",
            //        Encoding.GetEncoding("GBK"));
            //}
            //else
            //{
            //    if (a.ToLower().Equals("checkticket"))
            //    {
            //        var requestData = JsonConvert.DeserializeObject<CheckticketRequest>(request.ToString());
            //        //DataService.Instance.AddRecord($"收到验票数据:{JsonConvert.SerializeObject(requestData)}");

            //        var Checkticket = true;//ConfigurationHelper.GetInstance().GetBoolValueFromAppsettings("Checkticket");

            //        if (Checkticket)
            //        {
            //            var response = new CheckticketResponse()
            //            {
            //                checkResult = 1,
            //                checkType = 0,
            //                VoiceNum = 1,
            //                checkMsg1 = "票类：成人票",
            //                checkMsg2 = "票号：000000000001",
            //                checkMsg3 = "到期：2017-12-01",
            //                checkMsg4 = "验票成功，请过闸"
            //            };

            //            //// DataService.Instance.AddRecord($"\r\n");
            //            var json = JsonConvert.SerializeObject(response);

            //            //DataService.Instance.AddRecord($"发送验票数据:{json}");

            //            //Thread.Sleep(1500);

            //            return Content(JsonConvert.SerializeObject(json, Formatting.None), "application/json;charset=GBK",
            //                Encoding.GetEncoding("GBK"));
            //        }
            //        else
            //        {
            //            var response = new CheckticketResponse()
            //            {
            //                checkResult = 0,
            //                checkType = 0,
            //                VoiceNum = 1,
            //                checkMsg1 = "",
            //                checkMsg2 = "",
            //                checkMsg3 = "",
            //                checkMsg4 = "验票失败"
            //            };

            //            //OBJ转化成JSON
            //            string json = JsonConvert.SerializeObject(response);

            //            //   DataService.Instance.AddRecord($"\r\n");
            //            //DataService.Instance.AddRecord($"发送验票数据:{json}");

            //            //返回json数

            //            return Content(JsonConvert.SerializeObject(json, Formatting.None), "application/json;charset=GBK",
            //                Encoding.GetEncoding("GBK"));
            //        }
            //    }
            //    else
            //    {
            //        if (a.ToLower().Equals("cancellation"))
            //        {
            //            //DataService.Instance.AddRecord($"收到数据:{JsonConvert.SerializeObject(request)}");

            //            var response = new CancellationReponse()
            //            {
            //                checkResult = "OK"
            //            };

            //            //OBJ转化成JSON
            //            string json = JsonConvert.SerializeObject(response);

            //            // DataService.Instance.AddRecord($"\r\n");
            //            //DataService.Instance.AddRecord($"发送数据:{json}");

            //            //返回json数
            //            return Content(JsonConvert.SerializeObject(json, Formatting.None), "application/json;charset=GBK",
            //                Encoding.GetEncoding("GBK"));
            //        }
            //        else
            //        {
            //            var response = new CheckticketResponse()
            //            {
            //                checkResult = 0,
            //                checkType = 0,
            //                VoiceNum = 1,
            //                checkMsg1 = "",
            //                checkMsg2 = "",
            //                checkMsg3 = "",
            //                checkMsg4 = "无效的请求"
            //            };

            //            //OBJ转化成JSON
            //            string json = JsonConvert.SerializeObject(response);

            //            //返回json数
            //            return Content(JsonConvert.SerializeObject(json, Formatting.None), "application/json;charset=GBK",
            //                Encoding.GetEncoding("GBK"));
            //        }
            //    }
            //}
        }

        ///// <summary>
        /////消票
        ///// </summary>
        ///// <param name="request"></param>
        ///// <returns></returns>
        //[HttpPost]
        //public CancellationReponse Cancellation([FromQuery] string m, string a, [FromBody] CancellationRequest request)
        //{
        //    //DataService.Instance.AddRecord($"\r\n");
        //    //DataService.Instance.AddRecord($"收到数据:{JsonConvert.SerializeObject(request)}");

        //    //var Checkticket = ConfigurationHelper.GetInstance().GetBoolValueFromAppsettings("Checkticket");

        //    // if (Checkticket)
        //    //   {
        //    var response = new CancellationReponse()
        //    {
        //        checkResult = "OK"
        //    };

        //    //DataService.Instance.AddRecord($"\r\n");
        //    //DataService.Instance.AddRecord($"发送数据:{JsonConvert.SerializeObject(response)}");
        //    return response;
        //    //   }
        //    //    else
        //    //    {
        //    //      var   response = new CheckticketResponse()
        //    //        {
        //    //            checkResult = 0,
        //    //            checkType = 0,
        //    //            VoiceNum = 0,
        //    //            checkMsg1 = "",
        //    //            checkMsg2 = "",
        //    //            checkMsg3 = "",
        //    //            checkMsg4 = "fail"
        //    //        };

        //    //    DataService.Instance.AddRecord($"\r\n");
        //    //    DataService.Instance.AddRecord($"发送数据:{JsonConvert.SerializeObject(response)}");
        //    //    return response;
        //    //}
        //}
    }
}